﻿using Aribaccio;
using BDPAutodetect.Device;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// TargetSlaveDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class TargetSlaveDialog : Window, IDialog<QueryTargetSlaveDto>
    {
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        protected bool isEnabledPermission;
        /// <summary>
        /// 显示标题
        /// </summary>
        [ObservableProperty]
        protected string titleName = "指标";
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 查询当前工位
        /// </summary>
        [ObservableProperty]
        protected MStation stationItem;
        /// <summary>
        /// 查询工位集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MStation> stationItems;
        /// <summary>
        /// 查询当前从机设备
        /// </summary>
        [ObservableProperty]
        protected MSlave slaveItem;
        /// <summary>
        /// 查询从机设备集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MSlave> slaveItems;

        /// <summary>
        /// 原数据
        /// </summary>
        protected QueryTargetSlaveDto SourceData { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public QueryTargetSlaveDto? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public TargetSlaveDialog(string argTitleName, QueryTargetSlaveDto argQueryData, ObservableCollection<MStation> agrStations, ObservableCollection<MSlave> argSlaves)
        {
            InitializeComponent();
            this.TitleName = string.Concat(this.TitleName, "-", argTitleName);
            this.SourceData = argQueryData;
            this.StationItems = agrStations;
            this.SlaveItems = argSlaves;      
            this.SlaveItem = this.SlaveItems.First(v => v.ID == argQueryData.SlaveID);
            this.StationItem = this.StationItems.First(v => v.StationType == argQueryData.StationType);
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (this.StationItem == null)
            {
                this.StatusMsg = "工位初始化异常";
                return;
            }
            if (this.SlaveItem == null)
            {
                this.StatusMsg = "从机设备初始化异常";
                return;
            }
            if (this.SourceData != null)
            {
                if (this.SourceData.StationType == this.StationItem.StationType && this.SourceData.SlaveID == this.SlaveItem.ID)
                {
                    this.StatusMsg = "不能与原条件相同";
                    return;
                }
            }

            this.DialogResult = true;
            this.DialogResultData = new QueryTargetSlaveDto()
            {
                SlaveID = this.SlaveItem.ID,
                StationID = this.StationItem.ID,
                StationType = this.StationItem.StationType
            };
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
